﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using BlockMix.GamePanels;
using BlockMix.Utilities;

namespace BlockMix
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            
            
            WindowStyle = WindowStyle.None;
            WindowState = WindowState.Maximized;

            var screenSize = AppContext.ScreenSize;
            var appSize = AppContext.ApplicationSize;
            var margins = new Thickness((screenSize.Width - appSize.Width)/2, (screenSize.Height - appSize.Height)/2,
                                        (screenSize.Width - appSize.Width)/2, (screenSize.Height - appSize.Height)/2);

            // Loading panel
            panelLoading.Width = appSize.Width;
            panelLoading.Height = appSize.Height;
            panelLoading.Margin = margins;
            // Menu panel
            panelMenu.Width = appSize.Width;
            panelMenu.Height = appSize.Height;
            panelMenu.Margin = margins;
            // Play panel
            panelPlay.Width = appSize.Width;
            panelPlay.Height = appSize.Height;
            panelPlay.Margin = margins;

            background.SetValue(Canvas.LeftProperty, margins.Left);
            background.SetValue(Canvas.TopProperty, margins.Top);
            background.Width = appSize.Width;
            background.Height = appSize.Height;
            InitializeBackground();
        }

        #region Methods

        private void InitializeBackground()
        {
            //var aGeometryGroup = new GeometryGroup();
            //aGeometryGroup.Children.Add(new RectangleGeometry(new Rect(0, 0, 40, 40)));
            //aGeometryGroup.Children.Add(new RectangleGeometry(new Rect(40, 40, 40, 40)));
            
            var backgroundSquare1 = new GeometryDrawing(Brushes.LightGray, null,
                                            new RectangleGeometry(new Rect(0, 0, 40, 40)));
            var backgroundSquare2 = new GeometryDrawing(Brushes.DarkGray, null,
                                            new RectangleGeometry(new Rect(0, 39, 40, 40)));
            var diamond = new PathFigure(new Point(0, 0), new List<PathSegment>
                                                                {
                                                                    new LineSegment(new Point(20, 15), true),
                                                                    new LineSegment(new Point(25, 20), true),
                                                                    new LineSegment(new Point(20, 25), true),
                                                                    new LineSegment(new Point(15, 20), true)
                                                                }, true);
            var diamondSquare1 = new PathGeometry(); //(new EnumerableQuery<PathFigure>(a), Brushes.DarkSlateGray);
            diamondSquare1.Figures.Add(diamond);
            var a = new GeometryDrawing(Brushes.DarkSlateGray, null, diamondSquare1);
            //var diamondSquare1 = new PathGeometry(diamond, Brushes.DarkSlateGray);
            
            var drawGroup = new DrawingGroup();
            drawGroup.Children.Add(backgroundSquare1);
            drawGroup.Children.Add(backgroundSquare2);
            //drawGroup.Children.Add(a);

            var drawBrush = new DrawingBrush();
            drawBrush.Drawing = drawGroup;

            var gameBlockSize = AppContext.GameBlockSize;

            drawBrush.Viewport = new Rect(0, 0, (double)(1/gameBlockSize.Width), (double)(2/gameBlockSize.Height));
            drawBrush.TileMode = TileMode.Tile;

            background.Fill = drawBrush;
        }

        private void ShowGamePanel(FrameworkElement element)
        {
            element.Visibility = Visibility.Visible;
        }

        private void HideGamePanel(FrameworkElement element)
        {
            element.Visibility = Visibility.Collapsed;
        }

        #endregion

        #region Event handlers

        private void LoadingPanel_Success(object sender, EventArgs e)
        {
            HideGamePanel(panelLoading);
            ShowGamePanel(panelMenu);
        }

        private void LoadingPanel_Failed(object sender, EventArgs e)
        {
            Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            panelLoading.Start();
        }

        private void MenuPanel_Clicked(MenuPanel sender, MenuPanel.MenuOption option)
        {
            switch (option)
            {
                case MenuPanel.MenuOption.SinglePlayer:
                case MenuPanel.MenuOption.Battle:
                    HideGamePanel(panelMenu);
                    ShowGamePanel(panelPlay);
                    break;
                case MenuPanel.MenuOption.Settings:
                    break;
                case MenuPanel.MenuOption.Quit:
                    Close();
                    break;
            }
        }

        #endregion

    }
}
